package com.nd.qa.shareplate.script.macaca;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.nd.qa.shareplate.model.Module;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.model.Project;
import com.nd.qa.shareplate.model.ProjectType;
import com.nd.qa.shareplate.util.FileUtil;
/**
 * 创建脚本工具类
 * @author huangjian
 *
 */
public class MacacaScriptTool {

	private Module module = null;
	private Project project = null;
	private String scriptPath = null;
	private String userName = null;
	private String userPwd = null;
	//private String packageName = null;
	//private String activityName = null;
	private StringBuilder content = null;
	private OutputStreamWriter outputStreamWriter = null;
	InitMacacaScript macaca = new InitMacacaScript();
	//
	private ProjectType projectType = null;
	
	/**
	 * 
	 * @param module 模块信息
	 * @param project	项目信息
	 * @param projectDirPath	创建的测试脚本路径
	 */
	public MacacaScriptTool(Module module,Project project,ProjectType pType, String projectDirPath) {
		this.module = module;
		this.project = project;
		//this.operational = Util.dao.getOperational();
		this.scriptPath = projectDirPath;
		this.projectType = pType;
		//this.userName = pType.getProjectUserName();
		//this.userPwd = pType.getProjectUserPwd();
		//this.packageName = this.project.getProjectPackage();
		//this.activityName = this.project.getProjectMainActivity();
	}
	
	/**
	 * 写入脚本具体内容
	 * @param scriptList
	 */
	public void generateScript(List<List<PageElement>> scriptList) {
		CreateMacacaCase cMacacaCase = new CreateMacacaCase();
		createScript("MainActivity",module);
		try {
			for (List<PageElement> list : scriptList) {
				StringBuilder sb = new StringBuilder();
				PageElement element = list.get(0);
				String caseName = String.format("%s%s", element.getElementTypeName(), element.getElementId());
				System.out.println("-----------------------------------------");
				int size = list.size();
				for (int i = size - 1; i >= 0; i--) {
					sb.append(writeCode(list.get(i)));
					System.out.println(list.get(i).toString());
				}
				content.append(cMacacaCase.createCase(caseName, sb));
			}
			endWrite();
		} catch (Exception e) {
			endWrite();
			e.printStackTrace();
		}
	}
	
	/**
	 * 在本地创建脚本文件并初始化
	 * @param scriptName
	 */
	private void createScript(String scriptName,Module module){
	
		content = new StringBuilder();
		FileUtil fileUtil = new FileUtil();
		String path = this.project.getProjectPackage().replaceAll("\\.", "/");
		System.out.println("path:"+path);
		path = this.scriptPath;//+"src/"+path+"/test/"
		fileUtil.createDir(path);
		String fileName = "";
		if (module.getModuleOrder()!=null) {
			fileName = path + scriptName+"Test"+module.getModuleOrder();
		}else{
			fileName = path + scriptName+"Test";
		}
		File file = fileUtil.createFile(fileName, ".java");
		try {
			String moduleName =  this.module.getModuleName();
			outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
			outputStreamWriter.write(macaca.initMacacaScript(this.module,projectType,file.getName().split("\\.")[0],project));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 根据元素信息，写入脚本
	 * @param element
	 * @return
	 */
	private String writeCode(PageElement element) {
		
		StringBuilder sb = new StringBuilder();
		String operate = element.getElementActionName();
		if (operate.contains("scroll")&&!operate.equals("dragForView")) {
			sb.append(macaca.scroll(element));
		}else {
			switch (operate) {
			case "click":
				sb.append(macaca.click(element));
				//sb.append(macaca.assertElement(element));
				break;
			case "clickPoint":
				sb.append(macaca.clickPoint(element));
				//sb.append(macaca.assertElement(element));
				break;
			case "longClick":
				sb.append(macaca.longClick(element));
				//sb.append(macaca.assertElement(element));
				break;
			case "input":
				sb.append(macaca.sendKey(element));
				//sb.append(macaca.assertElement(element));
				break;
			case "query":
				sb.append(macaca.query(element));
				break;
			case "queryNotExist":
				sb.append(macaca.queryNotExist(element));
				break;
			case "back":
				sb.append(macaca.back(element));
				break;
			case "keys":
				sb.append(macaca.keys(element));
				break;
			case "queryByKeyBoard":
				sb.append(macaca.queryByKeyBoard(element));
				break;	
			case "dragForView":
				sb.append(macaca.dragByView(element));
				break;	
			default:
				break;
			}
		}
		return sb.toString();
	}
	
	private void endWrite(){
		if (outputStreamWriter != null) {
			try {
				outputStreamWriter.write(content.toString());
				outputStreamWriter.write("}");
				outputStreamWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
